# LogViewer class - Implements dialog that allows user to see logged events.
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import sys
import os
import string

try:
 	import pygtk
  	pygtk.require("2.4")
except:
  	pass
try:
	import gtk
  	import gtk.glade
except:
	sys.exit(1)
	
class ConflictResolveDialog:
    """
    Dialog that allows to select which of the conflicting recordings will be recorded
    
    More documentation here...
    """
    
    # Class attributes
    dialog = None
    widgets = None
    glade_file = "extra-tools/glade/resolve_conflict_dialog.glade"
    
    def __init__(self):
        """Create dialog and show it"""
        try:
            self.widgets = gtk.glade.XML(self.glade_file)
        except:
            self.logger.critical("Couldn't open glade file: " + self.gladefile)
            sys.exit(1)      
            
        callback_dic = { "on_apply_button_clicked" : self.on_apply_button_clicked }  
        self.widgets.signal_autoconnect(callback_dic)
        
        # Get conflicting recordings and create radiobutton selection for them
        self.create_selection_list()
        
        # Show dialog
        self.dialog = self.widgets.get_widget("ResolveDialog")
        self.dialog.show()
    
    def create_selection_list(self):
        # Create log treeview
        treeview = self.widgets.get_widget("treeview_conflicts")
        cell_renderer1 = gtk.CellRendererText()
        cell_renderer2 = gtk.CellRendererText()
        cell_renderer3 = gtk.CellRendererText()
        cell_renderer4 = gtk.CellRendererText()
        cell_renderer5 = gtk.CellRendererText()
        
        column1 = gtk.TreeViewColumn("Show")
        column1.pack_start(cell_renderer1, True)
        column1.set_attributes(cell_renderer1, text = 0)
        
        column2 = gtk.TreeViewColumn("Channel")
        column2.pack_start(cell_renderer2, True)
        column2.set_attributes(cell_renderer2, text = 1)
        
        column3 = gtk.TreeViewColumn("Date")
        column3.pack_start(cell_renderer3, True)
        column3.set_attributes(cell_renderer3, text = 2)
        
        column4 = gtk.TreeViewColumn("Begins")
        column4.pack_end(cell_renderer4, True)
        column4.set_attributes(cell_renderer4, text = 3)
        
        column5 = gtk.TreeViewColumn("Ends")
        column5.pack_end(cell_renderer5, True)
        column5.set_attributes(cell_renderer5, text = 4)
        
        treeview.append_column(column1)
        treeview.append_column(column2)
        treeview.append_column(column3)
        treeview.append_column(column4)
        treeview.append_column(column5)
        treeview.set_headers_visible(True)
        
        # Set model to view
        conflicts_list = gtk.ListStore(str, str, str, str, str)
        treeview.set_model(conflicts_list)
        
        #FIXME: Read real conflicts
        
        #Debug stuff below
        conflicts_list.append(['Friends','SubTV','21.5.2007','13.30','14.00'])
        conflicts_list.append(['Seinfeld','Nelonen','21.5.2007','13.20','13.50'])

    def on_apply_button_clicked(self, widget):
        """Resolve conflict (user has clicked Apply)"""
        #FIXME: remove conflicting recordings form database
        self.dialog.hide()
        self.dialog.destroy()
        pass
